import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:useable_app/eventBus/EventBus.dart';
import 'package:useable_app/global/Global.dart';
import '../model/AccountingAddModel.dart';
import '../utils/alert_utils.dart';
import '../model/AccountingModel.dart';
import '../global/GlobalTheme.dart';

class AccountingAddViewModel extends ChangeNotifier {
  List<Map> _types = [
    {"name": "收入", "id": 0},
    {"name": "支出", "id": 1}
  ];
  int _type = 0; //类型选中下标
  List _modes = []; //方式集合
  Map? _mode; //选中的方式对象

  List<Map> get getTypes {
    return _types;
  }

  get currentTypeName {
    return _types[getType]['name'];
  }

  get currentModeName {
    return getMode != null ? getMode!['name']: "";
  }

  int get getType {
    return _type;
  }

  void setType(int val) {
    _type = val;
    notifyListeners();
  }

  List get getModes {
    return _modes;
  }

  void setModes(List list) {
    _modes = list;
    notifyListeners();
  }

  Map? get getMode {
    return _mode;
  }

  void setMode(mode) {
    _mode = mode;
    notifyListeners();
  }

  void getModeAndAlert() async {
    if(getModes.length > 0) {
      var val = await showObjectAlertDialog(getModes, "选择方式", "name");
      if (val != null && getMode != getModes[val]) {
        setMode(getModes[val]);
      }
      return;
    }

    var result = await getAccountingType(_types[_type]["id"]);
    print(result.data);
    if (result.data["success"]) {
      _modes = result.data["data"];
      var val = await showObjectAlertDialog(getModes, "选择方式", "name");
      if (val != null && getMode != getModes[val]) {
        setMode(getModes[val]);
      }
    } else {
      EasyLoading.showError(result.data["msg"]);
    }
  }

  void insert(String money, String desc) async {
    Map map = {
      "t_id": _mode!["id"],
      "u_id": Global().getInstance().user["id"],
      "action": _type,
      "money": money,
      "desc": desc,
      "date": getYMD(DateTime.now()),
    };
    var result = await insertAccouting(json.encode(map));
    if (result.data["success"]) {
      EasyLoading.showSuccess(result.data["msg"]);
      // setMode(null);
      setMode({});
      setType(0);
      bus.emit("accouting_add_result");
    } else {
      EasyLoading.showError(result.data["msg"]);
    }
  }


}

String getYMD(DateTime dateTime) {
  return dateTime == null
      ? ""
      : dateTime.year.toString() +
      "-" +
      dateTime.month.toString() +
      "-" +
      dateTime.day.toString();
}